package com.hzqy.commons.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class YhzHttpUtils {
    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcDTMxoPDLYe558wJjytEUrXsZ\n" +
            "dutBiHncoF0qdBtkI5fm+MyGMgd+WT9cRgFiRD7KOs1UYwQnaYKp3Qi1/O9IQpQD\n" +
            "JCXA9Lf8Us4StU+YYJbs8xCYlVzNvk5ZB/gaPdwhR8ikwqqjJKlfMDFkvi52u0Ew\n" +
            "eMqObhsAwX3KKL8+EwIDAQAB";
    private static final Log LOG = LogFactory.getLog(YhzHttpUtils.class);

    public static Map<String, String> itfYhzData(String urlStr,String itv_id,String type) {
        HttpURLConnection con = null;
        Map<String, String> map = new HashMap<String, String>();
        String result = "";
        String success = "0";
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br =null;
        try {// 获取HttpURLConnection连接对象
            URL url = new URL(urlStr);
            con = (HttpURLConnection) url.openConnection();
            // 设置连接属性
            con.setDoOutput(true);
            con.setRequestMethod(type);
            con.setRequestProperty("version", "1.0");
            con.setRequestProperty("Content-Type", "text/xml");
            con.setRequestProperty("Pragma", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setConnectTimeout(10000);
            con.setReadTimeout(10000);

            long timestamp = System.currentTimeMillis();
            String str = itv_id+timestamp;
            String encodedData = RSAUtils.publicEncrypt(str, RSAUtils.getPublicKey(PUBLIC_KEY));
            con.setRequestProperty("timestamp", timestamp+"");
            con.setRequestProperty("Authori-zation", encodedData);

            int respCode = con.getResponseCode();
            if (respCode == 200) {
                success = "1";
                is = con.getInputStream();
                isr = new InputStreamReader(is,"UTF-8");
                br = new BufferedReader(isr);
               // BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
                String line = "";
                for (line = br.readLine(); line != null; line = br.readLine()) {
                    result+=line;
                }
            }else{
                success = "2";
                result = "response_status: " + respCode;
            }
        } catch (Exception e) {
            success = "2";
            //result = e.getMessage();

        }finally {

            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                }
            }
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                }
            }
            if(con != null)
                con.disconnect();
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }

        }
        map.put("result", result);
        map.put("success", success);
        return map;
    }

//    public static Map<String, String> postYhzData(String urlStr,String itv_id) {
//        HttpURLConnection con = null;
//        Map<String, String> map = new HashMap<String, String>();
//        String result = "";
//        String success = "0";
//        try {// 获取HttpURLConnection连接对象
//            URL url = new URL(urlStr);
//            con = (HttpURLConnection) url.openConnection();
//            // 设置连接属性
//            con.setDoOutput(true);
//            con.setRequestMethod("POST");
//            con.setRequestProperty("version", "1.0");
//            con.setRequestProperty("Content-Type", "text/xml");
//            con.setRequestProperty("Pragma", "no-cache");
//            con.setRequestProperty("Cache-Control", "no-cache");
//            con.setConnectTimeout(10000);
//            con.setReadTimeout(10000);
//
//            long timestamp = System.currentTimeMillis();
//            String str = itv_id+timestamp;
//            String encodedData = RSAUtils.publicEncrypt(str, RSAUtils.getPublicKey(PUBLIC_KEY));
//            con.setRequestProperty("timestamp", timestamp+"");
//            con.setRequestProperty("Authori-zation", encodedData);
//
//            int respCode = con.getResponseCode();
//            if (respCode == 200) {
//                success = "1";
//                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
//                String line = "";
//                for (line = br.readLine(); line != null; line = br.readLine()) {
//                    result+=line;
//                }
//                LOG.debug(result);
//            }else{
//                success = "2";
//                result = "response_status: " + respCode;
//            }
//        } catch (Exception e) {
//            success = "2";
//            result = e.getMessage();
//
//        }finally {
//            if(con != null)
//                con.disconnect();
//        }
//        map.put("result", result);
//        map.put("success", success);
//        return map;
//    }
//
//    public static Map<String, String> getYhzData(String urlStr,String itv_id) {
//        PrintWriter writer = null;
//        HttpURLConnection httpConn = null;
//        Map<String, String> result = new HashMap<String, String>();
//        String msg = "";
//        String success = "0";
//
//
//        try {// 获取HttpURLConnection连接对象
//            URL url = new URL(urlStr);
//            httpConn = (HttpURLConnection) url.openConnection();
//            // 设置连接属性
//            httpConn.setConnectTimeout(10000);
//            httpConn.setReadTimeout(10000);
//            httpConn.setDoInput(true);
//            httpConn.setDoOutput(true);
//            httpConn.setUseCaches(false);
//            httpConn.setRequestProperty("Connection", "Keep-Alive");
//            httpConn.setRequestProperty("Charset", "utf-8");
//            httpConn.setRequestProperty("Content-Type", "text/html");
//            httpConn.setRequestMethod("GET");
//
//            long timestamp = System.currentTimeMillis();
//            String str = itv_id+timestamp;
//            String encodedData = RSAUtils.publicEncrypt(str, RSAUtils.getPublicKey(PUBLIC_KEY));
//            httpConn.setRequestProperty("timestamp", timestamp+"");
//            httpConn.setRequestProperty("Authori-zation", encodedData);
//
//            int respCode = httpConn.getResponseCode();
//            if (respCode == 200) {
//                success = "1";
//                msg = getStringFromInputStream(httpConn.getInputStream());
//                LOG.debug(msg);
//            }else{
//                success = "2";
//                msg = "response_status: " + respCode;
//            }
//        } catch (Exception e) {
//            success = "2";
//            msg = e.getMessage();
//        }finally {
//            if(httpConn != null)
//                httpConn.disconnect();
//            close(writer);
//        }
//
//        result.put("result", msg);
//        result.put("success", success);
//        return result;
//    }
//
//
//    private static final void close(Closeable closeable) {
//        if (closeable != null) {
//            try {
//                closeable.close();
//            } catch (IOException e) {
//            }
//        }
//    }
//
//    public static String getStringFromInputStream(InputStream inputStream) throws UnsupportedEncodingException {
//        StringBuffer str = new StringBuffer();
//        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
//        try {
//            String temp;
//            while ((temp = br.readLine()) != null) {
//                str.append(temp);
//            }
//        } catch (IOException e) {
//        } finally {
//            close(inputStream);
//        }
//        return str.toString();
//    }

    public static void main(String args[]){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(1612195200);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        System.out.println(res);


        LOG.debug(""+itfYhzData("https://tyhz.yishengyue.cn/api/stb/yesterday/food?itv_id=12345654&time=1615392000","12345654","GET"));
        //LOG.debug(itfYhzData("https://tyhz.yishengyue.cn/api/stb/give/like?itv_id=12345654&food_id=1","12345654","POST"));
    }

}